from django.db.models import Q
from django.shortcuts import render, redirect
from django.http import JsonResponse

# Create your views here.
from projects.models import Projects, Modules
from pure_pagination.paginator import Paginator, PageNotAnInteger, EmptyPage


def project_list(request):
    nums_list = [5, 10, 15, 20]
    nums = request.GET.get('nums', None)
    projects_list = Projects.objects.all().order_by('-create_time')
    is_project = int(request.COOKIES['Project'])
    if nums:
        page_num = int(nums)
    else:
        page_num = 5
    try:
        page = request.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1
    search_text = request.GET.get('Search', '')
    search_dict = {}
    if search_text:
        search_dict['name__contains'] = search_text
    projects = Projects.objects.filter(**search_dict).order_by('-create_time')
    counts = projects.count()

    pag = Paginator(projects, page_num, request=request)
    try:
        obj_list = pag.page(page)
    except EmptyPage:
        obj_list = pag.page(1)
    content = {
        'obj_list': obj_list,
        'search_text': search_text,
        'counts': counts,
        'nums_list': nums_list,
        "projects_list": projects_list,
        "is_project": is_project
    }
    return render(request, 'project_list.html', content)


def project_add(request):
    project_name = request.POST['project_name']
    project_describe = request.POST['project_describe']
    project_creator = request.POST['project_creator']
    project_status = request.POST['project_status']
    if project_status == '运行中':
        project_status = True
    else:
        project_status = False
    if project_name != '' and project_describe != '' and project_creator != '' and project_status != '':

        pro = Projects.objects.filter(name=project_name).exists()
        if pro:
            return JsonResponse({"status": False, "error_msg": "项目已存在"})
        project = Projects(name=project_name, describe=project_describe, creator=project_creator, status=project_status)
        project.save()
        return JsonResponse({"status": True})
    return JsonResponse({"status": False, "error_msg": "信息不能为空"})


def project_del(request):
    project_id = request.POST['project_id']
    Projects.objects.filter(id=project_id).delete()
    return JsonResponse({"status": True})


def project_edit(request):
    project_id = request.POST['project_id']
    project_name = request.POST['project_name']
    project_describe = request.POST['project_describe']
    project_creator = request.POST['project_creator']
    project_status = request.POST['project_status']
    if project_status == '运行中':
        project_status = True
    else:
        project_status = False
    if project_name != '' and project_describe != '' and project_creator != '' and project_status != '':
        pro = Projects.objects.filter(Q(name=project_name) & ~Q(id=project_id)).exists()
        if pro:
            return JsonResponse({"status": False, "error_msg": "项目已存在"})
        pro2 = Projects.objects.filter(Q(name=project_name)).exists()
        if pro2:
            Projects.objects.filter(id=project_id).update(describe=project_describe,
                                                          creator=project_creator, status=project_status)
        else:
            Projects.objects.filter(id=project_id).update(name=project_name, describe=project_describe,
                                                          creator=project_creator, status=project_status)
        return JsonResponse({"status": True})
    return JsonResponse({"status": False, "error_msg": "信息不能为空"})


def module_list(request):
    nums_list = [5, 10, 15, 20]
    page_num = request.GET.get('nums', None)
    projects_list = Projects.objects.all().order_by('-create_time')
    is_project = int(request.COOKIES['Project'])
    if page_num:
        page_num = int(page_num)
    else:
        page_num = 5
    try:
        page = request.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1
    search_text = request.GET.get('Search', '')
    search_dict = {}
    if search_text:
        search_dict['name__contains'] = search_text
    search_dict['project_id'] = is_project
    modules = Modules.objects.filter(**search_dict)
    counts = modules.count()
    pag = Paginator(modules, page_num)
    try:
        obj_list = pag.page(page)
    except EmptyPage:
        obj_list = pag.page(1)
    content = {
        'obj_list': obj_list,
        'search_text': search_text,
        'counts': counts,
        'nums_list': nums_list,
        'projects_list': projects_list,
        'is_project': is_project,
    }
    return render(request, 'module_list.html', content)


def module_add(request):
    module_name = request.POST['module_name']
    module_describe = request.POST['module_describe']
    module_creator = request.POST['module_creator']
    module_project = request.POST['module_project']
    project_id = Projects.objects.get(name=module_project).id
    if (module_name or module_describe or module_creator or module_project) == '':
        return JsonResponse({'status': False, 'error_msg': '信息不能为空'})
    exists = Modules.objects.filter(Q(name=module_name) & Q(project=project_id)).exists()
    if exists:
        return JsonResponse({'status': False, 'error_msg': '该项目模块已存在'})
    module = Modules(name=module_name, describe=module_describe, creator=module_creator, project_id=project_id)
    module.save()
    return JsonResponse({'status': True})


def module_edit(request):
    module_name = request.POST['module_name']
    module_id = request.POST['module_id']
    module_describe = request.POST['module_describe']
    module_creator = request.POST['module_creator']
    module_project = request.POST['module_project']
    project_id = Projects.objects.get(name=module_project).id
    # 模块与项目存在但是id自己
    exists = Modules.objects.filter((Q(name=module_name) & Q(project_id=project_id) & ~Q(id=module_id))).exists()
    if exists:
        return JsonResponse({'status': False, 'error_msg': '该项目模块已存在'})
    Modules.objects.filter(id=module_id).update(name=module_name, project_id=project_id, describe=module_describe,
                                                creator=module_creator)
    return JsonResponse({'status': True})


def module_del(request):
    module_id = request.POST['module_id']
    Modules.objects.filter(id=module_id).delete()
    return JsonResponse({'status': True})
